<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*总数字资源类型model*/

class ResourceTotalType extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_total_type';



    /*关联书单书籍*/
    public function conResourceTotal()
    {
        return $this->belongsToMany(ResourceTotal::class, 'resource_total_type_intention', 'resource_guid', 'type_id', 'id', 'guid');
    }

    /**
     *获取推荐的书单
     */
    public function getResourceTotalDataList($big_type_id = null, $limit = 10)
    {
        $res = $this->select('id', 'type_name', 'img', 'intro', 'create_time')->where('is_del', 1)->orderByDesc('id')->limit($limit)->get()->toArray();

        $data = [];
        $i = 0;
        foreach ($res as $key => $val) {
            $resource_total_list = $this->getResourceTotalTypeDataListByTypeId($big_type_id, $val['id'], 6);
            if ($resource_total_list) {
                $val['con_resource_total'] = $resource_total_list;
                $data[$i] = $val;
                $i++;
            }
        }
        return $data;
    }

    /**
     *获取推荐的书单
     */
    public function getResourceTotalTypeDataListByTypeId($big_type_id = null, $type_id = null, $keywords = null, $limit = 10)
    {
        //获取敏感词
                // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();

        $resourceTotalModel = new ResourceTotal();
        $resource_total_list = $resourceTotalModel->from('resource_total as r')
            ->select('r.guid', 'r.resource_business_id', 'r.title', 'r.author', 'r.press', 'r.pre_time', 'r.isbn', 'r.img', 'r.intro', 'r.big_type_id', 't.type_name as big_type_name', 'b.name as resource_business_name')
            ->join('resource_total_type_intention as i', 'r.guid', '=', 'i.resource_guid')
            ->join('resource_type as t', 't.id', '=', 'r.big_type_id')
            ->join('resource_business_name as b', 'b.id', '=', 'r.resource_business_id')
            ->where(function ($query) use ($big_type_id) {
                if ($big_type_id) {
                    $query->where('r.big_type_id', $big_type_id); //系统大类
                }
            })
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('r.title', 'like', '%' . $keywords . '%');
                }
            })->where('is_mask', 1)
            ->where('r.is_del', 1)
            ->where('b.is_del', 1)
            ->where('t.is_del', 1)
            ->where('i.type_id', $type_id)
            ->orderByDesc('i.id')
            ->limit($limit)
            ->get()
            ->toArray();
        return $resource_total_list;
    }

    /**
     * 根据自定义类型获取资源列表
     * @param custom_type_id  自定义资源类型id  未传表示全部
     * @param keywords 关键字搜索
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getCustomTypeResourceLists($keywords = null, $custom_type_id = null, $limit = 10)
    {
        //获取敏感词
                // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();

        $resourceTotalTypeIntentionModel = new ResourceTotalTypeIntention();
        $res = $resourceTotalTypeIntentionModel->from($resourceTotalTypeIntentionModel->getTable() . ' as i')
            ->select([
                't.id',
                't.guid',
                't.resource_business_id',
                't.title',
                't.author',
                't.isbn',
                't.press',
                't.pre_time',
                't.img',
                't.intro',
                't.create_time',
                't.big_type_id',
                't.type_id',
                'to.type_name as big_type_name'
            ])
            ->join('resource_total as t', 't.guid', '=', 'i.resource_guid')
            ->join('resource_type as to', 'to.id', '=', 't.big_type_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('title', 'like', "%$keywords%");
                }
            })
            ->where(function ($query) use ($custom_type_id) {
                if ($custom_type_id) {
                    $query->where('i.type_id', $custom_type_id);
                }
            })->where('is_mask', 1)
            ->where('t.is_del', 1)
            ->orderByDesc('i.id')
            ->paginate($limit)
            ->toArray();
        return $res;
    }
}
